﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using Microsoft.ApplicationBlocks.Data;
using System.Text;
using CWCare.Modules.cwcProvider.Components;
using System.Data.Linq;

namespace CWCare.Modules.cwcProvider
{
    public partial class UpdateLocation : PortalModuleBase
    {
        private const string InsertLocation = "Insert Location";
        private const string EmptyProvider = "Empty Provider";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (this.IsEditable == false)
                {
                    string paraId = string.Empty;
                    if (!string.IsNullOrEmpty(Request.Params.Get("pid")))
                    {
                        paraId = "&pid=" + Request.Params.Get("pid");
                    }

                    Response.Redirect("~/Default.aspx?TabId=" + this.TabId + paraId);
                }

                this.SetLisrLocationId();
                this.SetLisrProviderId();
                this.SetStatusInsertLocation();

            }
        }

        private void SetStatusInsertLocation()
        {
            //btnUpdate.Text = "Insert";
            btnDelete.Visible = false;

            txtAddress.Text = string.Empty;
            txtLattitude.Text = string.Empty;
            txtLongitude.Text = string.Empty;
            txtCity.Text = string.Empty;
            txtState.Text = string.Empty;
            txtZip.Text = string.Empty;
            txtCountry.Text = string.Empty;
            txtPhone.Text = string.Empty;
            txtFax.Text = string.Empty;
            txtHours.Text = string.Empty;
            txtIsDexa.Text = string.Empty;
            txtDic.Text = string.Empty;
            txtIsAmb.Text = string.Empty;
        }

        private void SetStatusUpdateLocation(string saddId)
        {
            //btnUpdate.Text = "Update";
            btnDelete.Visible = true;

            this.GetLocation(int.Parse(saddId));
        }

        private void SetLisrLocationId()
        {
            cwcareDataContext context = new cwcareDataContext();
            var info = (from loc in context.GetTable<cwc_Location>()
                        select new ListItem(loc.address, loc.addressID.ToString()));

            ddlAddressId.DataSource = info;
            ddlAddressId.DataTextField = "text";
            ddlAddressId.DataValueField = "value";
            ddlAddressId.DataBind();
            ddlAddressId.Items.Insert(0, new ListItem(InsertLocation, InsertLocation));
        }

        private void SetLisrProviderId()
        {
            cwcareDataContext context = new cwcareDataContext();
            var info = (from pro in context.GetTable<cwc_Provider>()
                        select new ListItem((pro.fName != null ? pro.fName : "") + " " + (pro.lName == null ? "" : pro.lName), pro.providerID.ToString()));
            
            ddlProviderId.DataSource = info;
            ddlProviderId.DataTextField = "text";
            ddlProviderId.DataValueField = "value";
            ddlProviderId.DataBind();

            ddlProviderId.Items.Insert(0, new ListItem(EmptyProvider, EmptyProvider));
        }

        private int GetMaxLocationId()
        {
            cwcareDataContext context = new cwcareDataContext();
            var max = (from loc in context.GetTable<cwc_Location>()
                       select loc.addressID).Max();

            return max + 1;
        }

        private int GetIndexItem(DropDownList ddl, string value)
        {
            int index = -1;
            var info = (from a in ddl.Items.Cast<ListItem>()
                        where (a.Value == value || a.Text == value)
                        select a).ToList<ListItem>();
            if (info.Count > 0)
            {
                index = ddl.Items.IndexOf(info[0]);
            }
            return index;
        }

        private void GetLocation(int id)
        {
            cwcareDataContext context = new cwcareDataContext();
            var info = (from pro in context.GetTable<cwc_Location>()
                        where pro.addressID == id
                        select pro).ToList<cwc_Location>();
            if (info.Count > 0)
            {
                //ddlAddressId.SelectedIndex = ddlAddressId.Items.IndexOf(new ListItem(pid.ToString(), pid.ToString()));                
                ddlAddressId.SelectedIndex = this.GetIndexItem(ddlAddressId, id.ToString());
                txtAddress.Text = info[0].address;
                txtLattitude.Text = info[0].lattitude;
                txtLongitude.Text = info[0].longitude;
                txtCity.Text = info[0].city;
                txtState.Text = info[0].state;
                ddlProviderId.SelectedIndex = this.GetIndexItem(ddlProviderId, info[0].providerID.ToString());
                txtZip.Text = info[0].zip.ToString();
                txtCountry.Text = info[0].county;
                txtPhone.Text = info[0].phone;
                txtFax.Text = info[0].fax;
                txtHours.Text = info[0].loc_hours;
                txtIsDexa.Text = info[0].isDexa;
                txtDic.Text = info[0].dic.ToString();
                txtIsAmb.Text = info[0].isAmb;
            }
            else
            {
                this.SetStatusInsertLocation();
            }
        }

        private void SetLocation(cwc_Location loc)
        {
            loc.address = txtAddress.Text;
            loc.lattitude = txtLattitude.Text;
            loc.longitude = txtLongitude.Text;
            loc.city = txtCity.Text;
            loc.state = txtState.Text;
            if (!string.IsNullOrEmpty(txtZip.Text))
                loc.zip = Convert.ToInt32(txtZip.Text);
            loc.county = txtCountry.Text;
            loc.phone = txtPhone.Text;
            loc.fax = txtFax.Text;
            if (!ddlProviderId.SelectedValue.Equals(EmptyProvider))
                loc.providerID = int.Parse(ddlProviderId.SelectedItem.Value);
            loc.loc_hours = txtHours.Text;
            loc.isDexa = txtIsDexa.Text;
            if (!string.IsNullOrEmpty(txtDic.Text))
                loc.dic = Convert.ToInt32(txtDic.Text);
            loc.isAmb = txtIsAmb.Text;
        }

        protected void btnUpdate_OnClick(object sender, EventArgs e)
        {
            cwcareDataContext context = new cwcareDataContext();
            if (ddlAddressId.SelectedValue.Equals(InsertLocation))
            {
                cwc_Location loc = new cwc_Location();
                this.SetLocation(loc);
                //loc.addressID = this.GetMaxLocationId();
                context.cwc_Locations.InsertOnSubmit(loc);
                try
                {
                    context.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                }
            }
            else
            {
                int addId = int.Parse(ddlAddressId.SelectedValue);
                var info = (from loc in context.GetTable<cwc_Location>()
                            where loc.addressID == addId
                            select loc).ToList<cwc_Location>();
                if (info.Count > 0)
                {
                    cwc_Location loc = info[0];
                    this.SetLocation(loc);
                    try
                    {
                        context.SubmitChanges();
                    }
                    catch (ChangeConflictException)
                    {
                        context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                    }
                }
            }

            string paraId = string.Empty;
            if (!string.IsNullOrEmpty(Request.Params.Get("pid")))
            {
                paraId = "&pid=" + Request.Params.Get("pid");
            }

            Response.Redirect("~/Default.aspx?TabId=" + this.TabId + paraId);
        }

        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            int pid = -1;                 
            if (!ddlAddressId.SelectedValue.Equals(InsertLocation))
            {
                pid = int.Parse(ddlAddressId.SelectedValue);
                cwcareDataContext context = new cwcareDataContext();
                var info = (from pro in context.GetTable<cwc_Location>()
                            where pro.addressID == pid
                            select pro).ToList<cwc_Location>();
                if (info.Count > 0)
                {
                    cwc_Location loc = info[0];
                    context.cwc_Locations.DeleteOnSubmit(loc);
                    try
                    {
                        context.SubmitChanges();
                    }
                    catch (ChangeConflictException)
                    {
                        context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                    }
                }                
            }

            string paraId = string.Empty;
            if (!string.IsNullOrEmpty(Request.Params.Get("pid")))
            {
                paraId = "&pid=" + Request.Params.Get("pid");
            }

            Response.Redirect("~/Default.aspx?TabId=" + this.TabId + paraId);

        }

        protected void btnCancel_OnClick(object sender, EventArgs e)
        {
            string paraId = string.Empty;
            if (!string.IsNullOrEmpty(Request.Params.Get("pid")))
            {
                paraId = "&pid=" + Request.Params.Get("pid");
            }

            Response.Redirect("~/Default.aspx?TabId=" + this.TabId + paraId);
        }

       
        protected void ddlAddressId_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlAddressId.SelectedValue.Equals(InsertLocation))
            {
                this.SetStatusInsertLocation();
            }
            else
            {
                this.SetStatusUpdateLocation(ddlAddressId.SelectedValue);
            }
        }
       
    }

}


